export default class Rectangle {
    constructor({ color, strokeColor = '#ff0000' ,startX, startY, canvasCtx, imgUrl }) {
        this.color = color;
        this.strokeColor = strokeColor;
        this.startX = startX;
        this.startY = startY;
        this.endX = startX;
        this.endY = startY;
        this.canvasCtx = canvasCtx;  // canvas上下文，用于绘制图形
        this.imgUrl = imgUrl;
    }

    get minX() {
        return Math.min(this.startX * 1 * 1, this.endX * 1 * 1);
    }
    get maxX() {
        return Math.max(this.startX * 1 * 1, this.endX * 1 * 1);
    }
    get minY() {
        return Math.min(this.startY * 1 * 1, this.endY * 1 * 1);
    }
    get maxY() {
        return Math.max(this.startY * 1 * 1, this.endY * 1 * 1);
    }

    // 判断某一个点是否在本图形中
    isInside(x, y) {
        x = x * 1 * 1;
        y = y * 1 * 1;
        return x >= this.minX && x <= this.maxX && y >= this.minY && y <= this.maxY;
    } 


    // 绘制图形
    draw() {



        this.canvasCtx.beginPath();
        this.canvasCtx.moveTo(this.minX, this.minY);
        this.canvasCtx.lineTo(this.maxX, this.minY);
        this.canvasCtx.lineTo(this.maxX, this.maxY);
        this.canvasCtx.lineTo(this.minX, this.maxY);
        this.canvasCtx.lineTo(this.minX, this.minY);
        // this.canvasCtx.fillStyle = this.color;
        // if (this.imgUrl) {
        //     let image = new Image();
        //     image.src = this.imgUrl;
        //     image.onload = () => {
        //         this.canvasCtx.drawImage(image, this.minX, this.minY, this.maxX - this.minX, this.maxY - this.minY);
        //     }
        // } else {
        // }
        this.canvasCtx.fillStyle = this.color;
        this.canvasCtx.fill();

        this.canvasCtx.strokeStyle = this.strokeColor;
        this.canvasCtx.lineWidth = 1;
        this.canvasCtx.lineCap = 'square'
        this.canvasCtx.stroke();
    }
}